package com.ywr;

import java.util.ArrayList;
import java.util.List;
import java.util.Optional;


public class OptionalUtil {

    /**
     * 判断输入的字符串是否为空
     * @param str String
     * @return
     */
    public static boolean isEmpty(String str){
        return  Optional.ofNullable(str).isPresent();
    }

    /**
     * 判断输入的Integer是否为空
     * @param num Integer
     * @return
     */
    public static boolean isEmpty(Integer num){
        return  Optional.ofNullable(num).isPresent();
    }
    /**
     * 判断输入的Double是否为空
     * @param dnum Double
     * @return
     */
    public static boolean isEmpty(Double dnum){
        return  Optional.ofNullable(dnum).isPresent();
    }
    /**
     * 判断输入的Object是否为空
     * @param obj
     * @return
     */
    public static boolean isEmpty(Object obj){
        return  Optional.ofNullable(obj).isPresent();

    }

    /**
     * 判断输入的List是否为空,获取该对象
     * @param obj
     * @return
     */
    public static Object get(Object obj){
        return Optional.ofNullable(obj).isPresent()!=false?Optional.ofNullable(obj).get():null;

    }
    /**
     * 测试
     * @param args
     */
    @SuppressWarnings("unchecked")
    public static void main(String[] args) {
        List<String> list = new ArrayList<>();
        list.add("a");
        list.add("b");
        list.add("c");
        List<String> objList = (List<String>) OptionalUtil.get(list);
        objList.forEach(item->{System.out.println(item);});

        String str = null;
        System.out.println("当前对象:"+OptionalUtil.get(str));


    }
}
